python自动化系列之提取pdf文字和图片 |
您所在的位置:网站首页 › 提取pdf中的文字 app › python自动化系列之提取pdf文字和图片 |
在python中有许多开源的库可以处理Pdf文档,最常用的Pypdf2库可以读取文档,合并,分割pdf文档,但是也有局限性: 无法提取文档中的文字提取PDF文字需要使用另外的库,如pdfplumbe提取PDF中的图片需要使用fitz库 使用pdfplumbe提取文字pdfplumbe使用可以用来解析PDF文件,获取其文本内容、标题、表格等的开源工具;开源代码地址:https://github.com/jsvine/pdfplumber 安装pdfplumbe: pip install pdfplumbe引入: import pdfplumbe简单使用代码示例: filepath = 'H:/test_w.pdf' def extract_text_info(filepath): """ 提取PDF中的文字 @param filepath:文件路径 @return: """ with pdfplumber.open(filepath) as pdf: # 获取第2页数据 page = pdf.pages[3] print(page.extract_text()) #提取文字 table = page.extract_tables() #提取表格 print(table) for row in table: print(row)pdfplumber提供了两种读取pdf的方式: pdfplumber.open("path/to/file.pdf") pdfplumber.load(file_like_object)这两种方法都返回pdfplumber.PDF类的实例(instance)。加载带密码的pdf需要传入参数password,例如: pdfplumber.open("file.pdf", password = "test") fitz的简单使用使用fitz需要同时安装fitz和PyMuPDF,否则会报错 安装: pip install fitz PyMupdf引入: import fitz 使用fitz将pdf转为图片: def pdf2img(): import fitz '''pdf转图片''' with fitz.open(filepath) as doc: for page in doc: mat = fitz.Matrix(2,2) pix = page.get_pixmap(matrix= mat) pix.save(f'H:/{page.number}.png') 使用fitz转换图片 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |